package com.supermap.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.iot.IotDubboConsumerService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xiaolu
 * @date 2018/6/8 上午10:28
 */
@RestController
@RequestMapping(value = "iot")
public class IotController {

    private final static String USER_NAME = "yunyang";
    private final static String PASSWORD = "Aa111111";
    private final static boolean USE_MD5 = true;

    @Resource
    IotDubboConsumerService iotDubboConsumerService;

    @RequestMapping(value = "getRegions")
    @ResponseBody
    public JSONObject getRegions() {
        return iotDubboConsumerService.getRegions(USER_NAME, PASSWORD, true);
    }

    @RequestMapping(value = "getNodesByRegionID")
    @ResponseBody
    public JSONObject getNodesByRegionID(@RequestBody Map param) {
        int regionId;
        if (param.get("regionID") instanceof String) {
            regionId = Integer.parseInt((String) param.get("regionID"));
        } else {
            regionId = (int) param.get("regionID");
        }
        return iotDubboConsumerService.getNodesByRegionID(USER_NAME, PASSWORD, USE_MD5, regionId);
    }

    @RequestMapping(value = "getNodesByBMID")
    @ResponseBody
    public JSONObject getNodesByBMID(@RequestBody Map param) {
        int bmId;
        if (param.get("bmId") instanceof String) {
            bmId = Integer.parseInt((String) param.get("bmId"));
        } else {
            bmId = (int) param.get("bmId");
        }
        return iotDubboConsumerService.getNodesByBMID(USER_NAME, PASSWORD, USE_MD5, bmId);
    }

    @RequestMapping(value = "getMonitorCategories")
    @ResponseBody
    public JSONObject getMonitorCategories(@RequestBody Map param) {
        int nodeId;
        if (param.get("nodeId") instanceof String) {
            nodeId = Integer.parseInt((String) param.get("nodeId"));
        } else {
            nodeId = (int) param.get("nodeId");
        }
        return iotDubboConsumerService.getMonitorCategories(USER_NAME, PASSWORD, USE_MD5, nodeId);
    }

    @RequestMapping(value = "getEnvInfoDetails")
    @ResponseBody
    public JSONObject getEnvInfoDetails(@RequestBody Map param) {
        int nodeId;
        JSONObject nodeData = new JSONObject();
        List nodeIdList = (List) param.get("nodeId");
        for (Object id : nodeIdList) {
            if (id instanceof String) {
                nodeId = Integer.parseInt((String) id);
            } else {
                nodeId = (int) id;
            }
            JSONObject data = iotDubboConsumerService.getEnvInfoDetails(USER_NAME, PASSWORD, USE_MD5, nodeId);
            if (data.getInteger("code") == 200 && data.get("data") != null) {
                nodeData.put(String.valueOf(nodeId), data.getJSONArray("data"));
            }
        }
        if (nodeData.keySet().size() > 0) {
            return assistMsg(200, nodeData);
        }
        return assistMsg(404, nodeData);
    }

    @RequestMapping(value = "getEnvInfosByStat")
    @ResponseBody
    public JSONObject getEnvInfosByStat(@RequestBody Map param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        int nodeId;
        int type;
        if (param.get("nodeId") instanceof String) {
            nodeId = Integer.parseInt((String) param.get("nodeId"));
        } else {
            nodeId = (int) param.get("nodeId");
        }
        if (param.get("type") instanceof String) {
            type = Integer.parseInt((String) param.get("type"));
        } else {
            type = (int) param.get("type");
        }

        String beginDateStr = (String) param.get("beginDate");
        String endDateStr = (String) param.get("endDate");
        if (endDateStr == null) {
            return iotDubboConsumerService.getEnvInfosByStat(USER_NAME, PASSWORD, USE_MD5, nodeId, beginDateStr, type);
        } else {
            try {
                JSONArray dataArray = new JSONArray();
                Date beginDate = sdf.parse(beginDateStr);
                Date endDate = sdf.parse(endDateStr);
                while (beginDate.before(endDate)) {
                    JSONObject data = iotDubboConsumerService.getEnvInfosByStat(USER_NAME, PASSWORD, USE_MD5, nodeId, sdf.format(beginDate), type);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(beginDate);
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
                    beginDate = calendar.getTime();
                    dataArray.addAll(data.getJSONArray("data"));
                }
                if (dataArray.size() > 0) {
                    return assistMsg(200, dataArray);
                }
                return assistMsg(404, dataArray);
            } catch (ParseException e) {
                return assistMsg(404, null, "ParseException");
            }
        }
    }

    private JSONObject assistMsg(int errorCode, Object data, String message) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", message);
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }

    /**
     * 错误信息
     *
     * @param errorCode 错误代码
     * @return 查询结果
     */
    private JSONObject assistMsg(int errorCode, Object data) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", "SERVICE GET AN EXCEPTION");
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }
}
